#define BOOST_TEST_MODULE testTemplateHelper

#include <filesystem>
#include <fstream>
#include <iostream>

#include <boost/test/unit_test.hpp>

#include "common/helpers/template_helper.h"

void createTestTemplates();
std::string resultTestTemplates();

BOOST_AUTO_TEST_CASE(testTemplateHelper01)
{
    Common::templateBaseDirectory() = "tmp/";
    std::filesystem::create_directories(Common::templateBaseDirectory());
    createTestTemplates();
    const auto content = Common::combineTemplates(
        "testTemplate01.txt",
        {
            { "valueA", "testTemplate03.txt" },
            { "valueB", "testTemplate03.txt" },
        }
    );
    BOOST_CHECK(resultTestTemplates() == content);
}

BOOST_AUTO_TEST_CASE(testTemplateHelper02)
{
    Common::templateBaseDirectory() = "tmp/223/";
    std::filesystem::create_directories(Common::templateBaseDirectory());
    createTestTemplates();
    const auto content = Common::combineTemplates(
        "testTemplate01.txt",
        {
            { "valueA", "testTemplate03.txt" },
            { "valueB", "testTemplate03.txt" },
        }
    );
    BOOST_CHECK(resultTestTemplates() == content);
}

BOOST_AUTO_TEST_CASE(testTemplateHelper03)
{
    Common::templateBaseDirectory() = "";
    createTestTemplates();
    const auto content = Common::combineTemplates(
        "testTemplate01.txt",
        {
            { "valueA", "testTemplate03.txt" },
            { "valueB", "testTemplate03.txt" },
        }
    );
    BOOST_CHECK(resultTestTemplates() == content);
}

void createTestTemplates()
{
    {
        std::ofstream file(
            Common::templateBaseDirectory()
            + "testTemplate01.txt"
        );
        if (file.is_open())
        {
            file
                << "text" << std::endl
                << "[[ testTemplate02.txt ]]" << std::endl
                << "text" << std::endl
                << "text" << std::endl
                << "[[ testTemplate03.txt ]]" << std::endl
                << "[[ testTemplate03.txt ]]" << std::endl
                << "text";
        }
    }

    {
        std::ofstream file(
            Common::templateBaseDirectory()
            + "testTemplate02.txt"
        );
        if (file.is_open())
        {
            file
                << "text2" << std::endl
                << "text_2" << std::endl
                << "text2"
                << "[[# valueA ]]222[[# valueB ]][[# valueC ]]";
        }
    }

    {
        std::ofstream file(
            Common::templateBaseDirectory()
            + "testTemplate03.txt"
        );
        if (file.is_open())
        {
            file
                << "text3" << std::endl
                << "3_text_3" << std::endl
                << "text3" << std::endl
                << " ";
        }
    }
}

std::string resultTestTemplates()
{
    std::stringstream result;
    result
        // testTemplate01.txt
        << "text" << std::endl

        // [[ testTemplate02.txt ]]
        << "text2" << std::endl
        << "text_2" << std::endl
        << "text2"
        // [[# valueA == testTemplate03.txt ]]
        << "text3" << std::endl
        << "3_text_3" << std::endl
        << "text3" << std::endl
        << " "
        // [[ testTemplate02.txt ]]
        << "222"
        // [[# valueA == testTemplate03.txt ]]
        << "text3" << std::endl
        << "3_text_3" << std::endl
        << "text3" << std::endl
        << " "

        // testTemplate01.txt
        << std::endl
        << "text" << std::endl
        << "text" << std::endl

        // [[ testTemplate03.txt ]]
        << "text3" << std::endl
        << "3_text_3" << std::endl
        << "text3" << std::endl
        << " "

        // [[ testTemplate03.txt ]]
        << std::endl
        << "text3" << std::endl
        << "3_text_3" << std::endl
        << "text3" << std::endl
        << " "

        // testTemplate01.txt
        << std::endl
        << "text";
    return result.str();
}
